part of authorized_login;

class AuthorizedWidget extends StatefulWidget {
  final String consumerKey;
  final String consumerSecret;
  final Color appleLogoColor;
  final Color appleLogoBgColor;
  final SuccessCallback? success;
  final FailureCallback? fail;
  final VoidCallback? toast;
  final bool turnOnTwitter;
  final Widget? googleWidget;

  const AuthorizedWidget({
    Key? key,
    required this.consumerKey,
    required this.consumerSecret,
    required this.appleLogoColor,
    required this.appleLogoBgColor,
    this.success,
    this.fail,
    this.toast,
    this.googleWidget,
    this.turnOnTwitter = true,
  }) : super(key: key);

  @override
  _AuthorizedWidgetState createState() => _AuthorizedWidgetState();
}

class _AuthorizedWidgetState extends State<AuthorizedWidget> {
  static const String loginGoogle = 'assets/login_google.png';
  static const String loginTwitter = 'assets/login_twitter.png';

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        _apple,
        _google,
        if (widget.turnOnTwitter) _twitter,
      ],
    );
  }

  Widget get _apple {
    return FutureBuilder(
        future: AuthorizedLogin().enableApple(),
        initialData: false,
        builder: (context, AsyncSnapshot<bool> snapShot) {
          if (snapShot.hasData && snapShot.data == true) {
            double height = 44;
            const _appleIconSizeScale = 28 / 44;
            final fontSize = height * 0.43;
            final appleIcon = Container(
              width: _appleIconSizeScale * height,
              height: _appleIconSizeScale * height + 2,
              padding: EdgeInsets.only(
                // Properly aligns the Apple icon with the text of the button
                bottom: (4 / 44) * height,
              ),
              child: Center(
                child: SizedBox(
                  width: fontSize * (25 / 31),
                  height: fontSize,
                  child: CustomPaint(
                    painter: AppleLogoPainter(
                      color: widget.appleLogoColor,
                    ),
                  ),
                ),
              ),
            );
            return Container(
              height: 36,
              width: 36,
              margin: const EdgeInsets.only(right: 48),
              child: CupertinoButton(
                minSize: 0,
                borderRadius: BorderRadius.circular(20),
                padding: EdgeInsets.zero,
                color: widget.appleLogoBgColor,
                child: appleIcon,
                onPressed: () {
                  AuthorizedLogin().signInWithApple(
                    callBack: ((value) => result(value)),
                    toast: widget.toast,
                  );
                },
              ),
            );
          }
          return const SizedBox();
        });
  }

  Widget get _google {
    return FutureBuilder(
        future: AuthorizedLogin().enableGoogle(),
        initialData: false,
        builder: (context, AsyncSnapshot<bool> snapShot) {
          if (snapShot.hasData && snapShot.data == true) {
            return Row(
              children: [
                GestureDetector(
                  onTap: () {
                    AuthorizedLogin()
                        .signInWithGoogle((value) => result(value));
                  },
                  child: widget.googleWidget ??
                      const ImgButton(
                        imgUrl: loginGoogle,
                      ),
                ),
              ],
            );
          }
          return Container();
        });
  }

  Widget get _twitter {
    return Container(
      margin: const EdgeInsets.only(left: 48),
      child: ImgButton(
        imgUrl: loginTwitter,
        onTap: () {
          AuthorizedLogin().signInWithTwitter(
            consumerKey: widget.consumerKey,
            consumerSecret: widget.consumerSecret,
            callBack: (value) => result(value),
            toast: widget.toast,
          );
        },
      ),
    );
  }

  void result(dynamic value) {
    if (value is UserEntity) {
      widget.success?.call(value);
      return;
    }
    widget.fail?.call(value);
  }
}

class ImgButton extends StatelessWidget {
  final double width;
  final double? height;
  final double radius;
  final String imgUrl;
  final VoidCallback? onTap;

  const ImgButton(
      {Key? key,
      this.width = 36,
      this.height,
      this.radius = 4,
      required this.imgUrl,
      this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: Image.asset(
          imgUrl,
          width: width,
          height: height ?? width,
          package: 'gkb_authorized_login',
        ),
      ),
    );
  }
}
